package game

import (
	"server/db"
	"server/gamedata"
	"server/msg"

	"google.golang.org/protobuf/proto"
)

type PlayerActiveMgr struct {
	M_Player *Player
}

func (p *PlayerActiveMgr) OpenNewLimitActivity(args []interface{}) {
	notify := &msg.NotifyLimitedTimeActiveData{}
	notify.MActiveList = []*msg.LimitedTimeActiveStruct{}

	id := args[0].(int)
	activityCfg := gamedata.GetConfigByName("Activity")
	record := activityCfg.Index(id).(*gamedata.ActivityRecord)

	act := &msg.LimitedTimeActiveStruct{}
	act.StartSvrTime = args[1].(int32)
	act.EndSvrTime = args[2].(int32)
	act.Type = int32(record.Type)
	ActiveID := args[3].(int32)
	if act.Type == 1 {

		if ActiveID == int32(0) {
			act.ActiveID = G_GameLogicPtr.InitPassportInsertDb(p.M_Player, act.StartSvrTime, act.EndSvrTime, id)
		} else {
			act.ActiveID = ActiveID
		}
		actor := &PlayerPassportData{PlayerActiveData: NewActiveData("PlayerPassportData", p.M_Player)}
		ok := actor.LoadDataFromDB(p.M_Player.M_DwUin)
		if ok {
			p.M_Player.playerdata["PlayerPassportData"] = actor
			p.M_Player.playerdataIF["PlayerPassportData"] = actor
		}

	}
	if act.Type == 2 {
		if ActiveID == int32(0) {
			act.ActiveID = G_GameLogicPtr.InitPromotionInsertDb(p.M_Player, act.StartSvrTime, act.EndSvrTime, id)
		} else {
			act.ActiveID = ActiveID
		}
		actor := &PlayerPromotionData{PlayerActiveData: NewActiveData("PlayerPromotionData", p.M_Player)}
		ok := actor.LoadDataFromDB(p.M_Player.M_DwUin)
		if ok {
			p.M_Player.playerdata["PlayerPromotionData"] = actor
			p.M_Player.playerdataIF["PlayerPromotionData"] = actor
		}
	}
	if act.Type == 3 {
		if ActiveID == int32(0) {
			act.ActiveID = G_GameLogicPtr.InitInfinitePackInsertDb(p.M_Player, act.StartSvrTime, act.EndSvrTime, id)
		} else {
			act.ActiveID = ActiveID
		}
		actor := &PlayerInfinitePackData{PlayerActiveData: NewActiveData("PlayerInfinitePackData", p.M_Player)}
		ok := actor.LoadDataFromDB(p.M_Player.M_DwUin)
		if ok {
			p.M_Player.playerdata["PlayerInfinitePackData"] = actor
			p.M_Player.playerdataIF["PlayerInfinitePackData"] = actor
		}
	}

	if act.Type == 4 {
		if ActiveID == int32(0) {
			act.ActiveID = G_GameLogicPtr.Init7DayLoginInsertDb(p.M_Player, act.StartSvrTime, act.EndSvrTime, id)
		} else {
			act.ActiveID = ActiveID
		}
		actor := &Player7DayLoginData{PlayerActiveData: NewActiveData("Player7DayLoginData", p.M_Player)}
		ok := actor.LoadDataFromDB(p.M_Player.M_DwUin)
		if ok {
			p.M_Player.playerdata["Player7DayLoginData"] = actor
			p.M_Player.playerdataIF["Player7DayLoginData"] = actor
		}
	}

	if act.Type == 5 {
		if ActiveID == int32(0) {
			act.ActiveID = G_GameLogicPtr.InitCardCollectInsertDb(p.M_Player, act.StartSvrTime, act.EndSvrTime, id)
		} else {
			act.ActiveID = ActiveID
		}
		actor := &PlayerCardCollectData{PlayerActiveData: NewActiveData("PlayerCardCollectData", p.M_Player)}
		ok := actor.LoadDataFromDB(p.M_Player.M_DwUin)
		if ok {
			p.M_Player.playerdata["PlayerCardCollectData"] = actor
			p.M_Player.playerdataIF["PlayerCardCollectData"] = actor
		}
	}

	notify.MActiveList = append(notify.MActiveList, act)
	data, _ := proto.Marshal(notify)
	agent := p.M_Player.GetAgentByPlayer()
	G_GameLogicPtr.PackResInfo(agent, "NotifyLimitedTimeActiveData", data)
}

func NewActiveData(dataName string, player *Player) *PlayerActiveData {
	act := &PlayerActiveData{
		PlayerData: NewPlayerData(dataName, player),
		MisDirty:   false,
	}

	return act
}

func (p *PlayerActiveMgr) InitActiveMgr(player *Player) {
	p.M_Player = player
	activityCfg := gamedata.GetConfigByName("Activity")
	map1 := make(map[int]string)
	map1[1] = "t_player_LimitPassport"
	map1[2] = "t_player_LimitPromotion"
	map1[3] = "t_player_LimitInfinitePack"
	map1[4] = "t_player_Limit7DayLogin"
	map1[5] = "t_player_card_data"
	for i := 0; i < len(G_GameLogicPtr.M_LimitActiveList); i++ {
		Id := G_GameLogicPtr.M_LimitActiveList[i]
		record := activityCfg.Index(Id).(*gamedata.ActivityRecord)
		sqlStr := "Select * From " + map1[record.Type] + " Where ConfigActId = ? And dwUin = ?"
		if record.Type == 1 {
			struct1 := []db.SqlLimitPassportStruct{}
			st := G_GameLogicPtr.GetActiveParamById(Id)
			Start := st.StartTime
			EndTime := st.EndTime
			if err := db.SqlDb.Select(&struct1, sqlStr, Id, player.M_DwUin); err != nil {
				p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
			} else {
				if len(struct1) == 0 {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
				} else {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, struct1[0].ActiveID})
				}
			}

		}
		if record.Type == 2 {
			struct1 := []db.SqlLimitPromotionStruct{}
			st := G_GameLogicPtr.GetActiveParamById(Id)
			Start := st.StartTime
			EndTime := st.EndTime
			if err := db.SqlDb.Select(&struct1, sqlStr, Id, player.M_DwUin); err != nil {
				p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
			} else {
				if len(struct1) == 0 {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
				} else {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, struct1[0].ActiveID})
				}
			}
		}
		if record.Type == 3 {
			struct1 := []db.SqlLimitInfinitePackStruct{}
			st := G_GameLogicPtr.GetActiveParamById(Id)
			Start := st.StartTime
			EndTime := st.EndTime
			if err := db.SqlDb.Select(&struct1, sqlStr, Id, player.M_DwUin); err != nil {
				p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
			} else {
				if len(struct1) == 0 {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
				} else {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, struct1[0].ActiveID})
				}
			}

		}
		if record.Type == 4 {
			struct1 := []db.SqlSevenDayLoginStruct{}
			st := G_GameLogicPtr.GetActiveParamById(Id)
			Start := st.StartTime
			EndTime := st.EndTime
			if err := db.SqlDb.Select(&struct1, sqlStr, Id, player.M_DwUin); err != nil {
				p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
			} else {
				if len(struct1) == 0 {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
				} else {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, struct1[0].ActiveID})
				}
			}

		}

		if record.Type == 5 {
			struct1 := []db.SqlCardCollectStruct{}
			st := G_GameLogicPtr.GetActiveParamById(Id)
			Start := st.StartTime
			EndTime := st.EndTime
			if err := db.SqlDb.Select(&struct1, sqlStr, Id, player.M_DwUin); err != nil {
				p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
			} else {
				if len(struct1) == 0 {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, int32(0)})
				} else {
					p.OpenNewLimitActivity([]interface{}{Id, Start, EndTime, struct1[0].ActiveID})
				}
			}

		}

	}
}
